package com.artifact.mit.mypoint.service.async;

import static com.artifact.mit.mypoint.service.util.FormatHelper.appendImageUrl;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.artifact.mit.mypoint.activity.MyPointBaseActivity;
import com.artifact.mit.mypoint.adapter.PromoAdapter;
import com.artifact.mit.mypoint.service.ServiceHandler;
import com.artifact.mit.mypoint.service.model.SetImage;
import com.artifact.mit.mypoint.service.model.api.Promo;
import com.artifact.mit.mypoint.service.model.api.Promo.Promos;

public class PromoAsync extends AsyncTask<String, Void, Promo> {
		
	private TextView promoText1;
	private TextView promoText2;
	private TextView promoText3;
	private TextView promoText4;
	private ImageView promoImage1;
	private ImageView promoImage2;
	private ListView promoList;
	private Context promoContext;
	
	private boolean isForDasboard;
	private ServiceHandler serviceHandler;
	
	public PromoAsync(Context context, TextView promoName1, TextView promoDesc1, ImageView promoImg1, 
			TextView promoName2, TextView promoDesc2, ImageView promoImg2, boolean forDasboard) {
		serviceHandler = new ServiceHandler(context);
		promoText1 = promoName1;
		promoText2 = promoDesc1;
		promoImage1 = promoImg1;
		promoText3 = promoName2;
		promoText4 = promoDesc2;
		promoImage2 = promoImg2;
		isForDasboard = forDasboard;
	}
	
	public PromoAsync(Context context, ListView list, boolean forDasboard) {
		promoContext = context;
		serviceHandler = new ServiceHandler(context);
		promoList = list;
		isForDasboard = forDasboard;
	}	
	
	private void setProgress(int visibility){
		MyPointBaseActivity activity = ((MyPointBaseActivity) promoContext);
		if (activity != null)
			activity.setProgressBar(visibility);
	}
	
	@Override
	protected void onPreExecute() {
		setProgress(View.VISIBLE);
	}
		
	@Override
	protected void onPostExecute(Promo result) {
		if (isForDasboard && result.getPromos() != null && result.getPromos().length >= 2) {
			for (int i=0; i<result.getPromos().length;) {
				promoText1.setText(result.getPromos()[i].getName());
				promoText2.setText(result.getPromos()[i].getDesc());
				new ImageLoader().execute(new SetImage(promoImage1, appendImageUrl(result.getPromos()[i].getUrlImage()), false));
				
				promoText3.setText(result.getPromos()[i+1].getName());
				promoText4.setText(result.getPromos()[i+1].getDesc());
				new ImageLoader().execute(new SetImage(promoImage2, appendImageUrl(result.getPromos()[i+1].getUrlImage()), false));	
				setProgress(View.GONE);
				break;
			}			
		} else {
			PromoAdapter promoAdapter = new PromoAdapter(promoContext);
			for (Promos promo : result.getPromos()) {
				Log.d("Image to Append", appendImageUrl(promo.getUrlImage()));
				promoAdapter.add(promo);
			}
			promoList.setAdapter(promoAdapter);
			setProgress(View.GONE);
		}
	}
		
	@Override
	protected Promo doInBackground(String... params) {
		try {
			return serviceHandler.promoService();
		} catch (Exception e) {
			Log.e(this.getClass().getName(), "Error Promo Service", e);
		}
		return null;
	}
}
